package com.kymatrix.mpo.plus.bpm.server.service.impl;

import com.kymatrix.mpo.plus.bpm.server.model.form.activity.QueryBpmActivityResp;
import com.kymatrix.mpo.plus.bpm.server.service.BpmActivityService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;


/**
 * BPM 活动实例 Service 实现类
 */
@Service
@Slf4j
@Validated
public class BpmActivityServiceImpl implements BpmActivityService {

    @Resource
    private HistoryService historyService;

    @Override
    public List<QueryBpmActivityResp> getActivityListByProcessInstanceId(String processInstanceId) {
        List<HistoricActivityInstance> activityList = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId).list();
        List<QueryBpmActivityResp> res = new ArrayList<>();
        for (HistoricActivityInstance historicActivityInstance : activityList) {
            res.add(QueryBpmActivityResp.fromEntity(historicActivityInstance));
        }
        return res;
    }

    @Override
    public List<HistoricActivityInstance> getHistoricActivityListByExecutionId(String executionId) {
        return historyService.createHistoricActivityInstanceQuery().executionId(executionId).list();
    }

}
